<?php

/*
 *меппер отвечающий за работу с таблицей месседжей
 */

class Model_MessageMapper{
    public function saveMessage($mesModel){$_SESSION['query']=0;
        $dbConnection = Control_DbConnection::getConnection();
        $query = "INSERT INTO message (author, date, text, adressat, autherName) VALUES 
                ('".$mesModel->getMesAuthor()."', '".$mesModel->getMesDate()."', '"
                .$mesModel->getMesText()."', '".$mesModel->getAdressat()."', '".$mesModel->getAutherName()."');";
        $dbConnection->dbQuery($query);
        if($dbConnection->getRes()){$_SESSION['query']++;
            return true;
        } else {
            View_View::setError(Model_Error::getDbQueryError());
            return false;
        }
    }
    public function getUsersMessage($id){$_SESSION['query']=0;
        $dbConnection = Control_DbConnection::getConnection();
        $query = "SELECT * FROM message WHERE adressat='".$id."';";
        $dbConnection->dbQuery($query);
        if ($dbConnection->getRes()){$_SESSION['query']++;
            while ($row = mysql_fetch_array($dbConnection->getRes(), MYSQL_ASSOC)){
                $obj[] = new Model_Message($row);
                
            }
            return $obj;
        }else{
            View_View::setError(Model_Error::getDbQueryError());
            return false;
        }
    }
}